package org.zwy.gradetrack.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.zwy.gradetrack.common.Result;
import org.zwy.gradetrack.entity.TeacherEvaluation;
import org.zwy.gradetrack.service.TeacherEvaluationService;

import java.util.List;
import java.util.Map;

/**
 * 教师评价管理控制器
 */
@RestController
@RequestMapping("/api/teacher-evaluations")
public class TeacherEvaluationController {

    @Autowired
    private TeacherEvaluationService teacherEvaluationService;

    /**
     * 获取教师评价列表
     */
    @GetMapping
    @PreAuthorize("hasAnyRole('SYSTEM_ADMIN', 'TEACHER','GRADUATE')")
    public Result<Page<TeacherEvaluation>> list(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) Long teacherId,
            @RequestParam(required = false) Long studentId,
            @RequestParam(required = false) Long indicatorId) {
        
        return Result.success(teacherEvaluationService.pageEvaluations(current, size, teacherId, studentId, indicatorId));
    }

    /**
     * 创建教师评价
     */
    @PostMapping
    @PreAuthorize("hasAnyRole('SYSTEM_ADMIN', 'TEACHER')")
    public Result<TeacherEvaluation> create(@RequestBody TeacherEvaluation evaluation) {
        try {
            return Result.success(teacherEvaluationService.createEvaluation(evaluation));
        } catch (RuntimeException e) {
            return Result.error(400, e.getMessage());
        }
    }

    /**
     * 更新教师评价
     */
    @PutMapping("/{id}")
    @PreAuthorize("hasAnyRole('SYSTEM_ADMIN', 'TEACHER')")
    public Result<TeacherEvaluation> update(
            @PathVariable Long id,
            @RequestBody TeacherEvaluation evaluation) {
        try {
            return Result.success(teacherEvaluationService.updateEvaluation(id, evaluation));
        } catch (RuntimeException e) {
            return Result.error(400, e.getMessage());
        }
    }

    /**
     * 删除教师评价
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('SYSTEM_ADMIN')")
    public Result<Void> delete(@PathVariable Long id) {
        if (!teacherEvaluationService.removeById(id)) {
            return Result.error(404, "评价数据不存在");
        }
        return Result.success();
    }

    /**
     * 获取教师评价详情
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAnyRole('SYSTEM_ADMIN', 'TEACHER','GRADUATE')")
    public Result<TeacherEvaluation> getById(@PathVariable Long id) {
        TeacherEvaluation evaluation = teacherEvaluationService.getById(id);
        if (evaluation == null) {
            return Result.error(404, "评价数据不存在");
        }
        return Result.success(evaluation);
    }

    /**
     * 获取教师评价统计信息
     */
    @GetMapping("/statistics")
    @PreAuthorize("hasAnyRole('SYSTEM_ADMIN', 'TEACHER','GRADUATE')")
    public Result<Map<String, Object>> getStatistics(
            @RequestParam(required = false) Long teacherId,
            @RequestParam(required = false) Long studentId,
            @RequestParam(required = false) Long indicatorId) {
        
        return Result.success(teacherEvaluationService.getStatistics(teacherId, studentId, indicatorId));
    }

    /**
     * 批量导入教师评价数据
     */
    @PostMapping("/batch")
    @PreAuthorize("hasRole('SYSTEM_ADMIN')")
    public Result<Void> batchImport(@RequestBody List<TeacherEvaluation> evaluations) {
        try {
            teacherEvaluationService.batchImport(evaluations);
            return Result.success();
        } catch (RuntimeException e) {
            return Result.error(400, e.getMessage());
        }
    }

    /**
     * 获取教师对学生的所有评价
     */
    @GetMapping("/teacher-student")
    @PreAuthorize("hasAnyRole('SYSTEM_ADMIN', 'TEACHER')")
    public Result<List<TeacherEvaluation>> getTeacherStudentEvaluations(
            @RequestParam Long teacherId,
            @RequestParam Long studentId) {
        
        return Result.success(teacherEvaluationService.getTeacherStudentEvaluations(teacherId, studentId));
    }

    /**
     * 获取学生对指标点的所有评价
     */
    @GetMapping("/student-indicator")
    @PreAuthorize("hasAnyRole('SYSTEM_ADMIN', 'TEACHER')")
    public Result<List<TeacherEvaluation>> getStudentIndicatorEvaluations(
            @RequestParam Long studentId,
            @RequestParam Long indicatorId) {
        
        return Result.success(teacherEvaluationService.getStudentIndicatorEvaluations(studentId, indicatorId));
    }
} 